package com.louis.iread.ui;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

import com.louis.iread.R;
import com.louis.iread.adapter.ClassifyAdapter;
import com.louis.iread.bean.ClassItem;
import com.louis.iread.bean.Classify;
import com.louis.iread.bean.Config;
import com.louis.iread.bean.WebsiteMgr;
import com.louis.iread.event.ReadEvent;
import com.louis.iread.skin.SkinMgr;
import com.louis.iread.utils.AppUtils;
import com.louis.iread.utils.ConstValue;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class ClassifyFrm extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_TAG = "classify";

    // TODO: Rename and change types of parameters
    private String tag;
    private List<ClassItem> mMale;
    private List<ClassItem> mFemale;
    GridView mMaleView;
    GridView mFemaleView;

    public ClassifyFrm() {
        // Required empty public constructor
    }

    public static ClassifyFrm newInstance(String param1) {
        ClassifyFrm fragment = new ClassifyFrm();
        Bundle args = new Bundle();
        args.putString(ARG_TAG, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            tag = getArguments().getString(ARG_TAG);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View contentView =  inflater.inflate(R.layout.fragment_classify, container, false);
        mMaleView = (GridView) contentView.findViewById(R.id.male_class);
        mMale = WebsiteMgr.getManager().getMainSite().getClassify().getMale();
        ClassifyAdapter maleAdapter = new ClassifyAdapter(this.getActivity(), R.layout.classify_item, mMale);
        mMaleView.setAdapter(maleAdapter);
        mMaleView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(view.getContext(), ClassActivity.class);
                intent.putExtra(ConstValue.CLASS_TYPE, mMale.get(position));
                view.getContext().startActivity(intent);
            }
        });

        mFemaleView = (GridView) contentView.findViewById(R.id.female_class);
        mFemale = WebsiteMgr.getManager().getMainSite().getClassify().getFemale();
        ClassifyAdapter femaleAdapter = new ClassifyAdapter(this.getActivity(), R.layout.classify_item, mFemale);
        mFemaleView.setAdapter(femaleAdapter);
        mFemaleView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(view.getContext(), ClassActivity.class);
                intent.putExtra(ConstValue.CLASS_TYPE, mFemale.get(position));
                view.getContext().startActivity(intent);
            }
        });
        return contentView;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(ReadEvent event) {
        switch (event.getEventType()) {
            case SWTCH_TO_DAY:
            case SWITCH_TO_NIGHT:
                if (mMaleView != null) {
                    mMaleView.invalidate();
                }
                if (mFemaleView != null) {
                    mFemaleView.invalidate();
                }
                break;
            default:
                break;
        }
    }

    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            return;
        }
        SkinMgr.getInstance().applyView(view);
    }

}
